package com.mobisys.live.controller.wechat;


import com.mobisys.live.dao.mapper.*;
import com.mobisys.live.dao.model.*;

import com.mobisys.live.response.Response;

import com.mobisys.live.response.live.WechatSimpleLiveResponseBody;
import com.mobisys.live.response.video.WechatSimpleVideoResponseBody;
import com.mobisys.live.response.video.WechatVideoResponseBody;

import com.mobisys.live.util.PictureUtil;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by zhuo on 2018-01-16.
 * 微信小程序点播接口
 */
@RestController
public class WechatVideoController {
    private Logger logger= LogManager.getLogger(WechatVideoController.class.getName());

    @Autowired
    private LiveMapper liveMapper;

    @Autowired
    private GroupInfoMapper groupInfoMapper;

    @Autowired
    private PictureMapper pictureMapper;

    @Autowired
    private GuestMapper guestMapper;

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private UserMapper userMapper;


    /**
     * 获取点播列表
     * @param page
     * @param size
     * @param type
     * @param sort

     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/0.1/wechat/video",method = RequestMethod.GET)
    public Response getVideoList(@RequestParam int page, @RequestParam int size, @RequestParam String type,
                                @RequestParam String sort, HttpServletRequest httpServletRequest) {
        Response response=new Response<List<WechatSimpleVideoResponseBody>>();



        int pageOffset=(page-1)*10;
        if(type.equals("0")){
            type=null;
        }else if(type.equals("1")){
            type="学术";
        }else if(type.equals("2")){
            type="晚会";
        }else {
            type=null;
        }
        List<Video> list=videoMapper.selectByPage(pageOffset,10,type,null);
        if(list==null){
            response.setRetureValue(LIST_IS_EMPTY);
            response.setRetureMsg(MSG_LIST_IS_EMPTY);
            return response;
        }
        List<WechatSimpleVideoResponseBody> liveResponseBodies=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Video video=list.get(i);
            WechatSimpleVideoResponseBody responseBody=new WechatSimpleVideoResponseBody();
            responseBody.setVideoId(video.getId());
            responseBody.setUserId(video.getUserId());
            responseBody.setVideoName(video.getTitle());

            //获取用户名
            User user=userMapper.selectByPrimaryKey(video.getUserId());
            responseBody.setUserName(user.getNickName());

            responseBody.setViewCount(video.getViewCount());
            responseBody.setLikeCount(video.getLikeCount());

            String avatorUrl=PictureUtil.getPictureUrlFromId(user.getHeadPicId(),pictureMapper);
            String coverUrl=PictureUtil.getPictureUrlFromId(video.getCoverPictureId(),pictureMapper);

            responseBody.setAvatorUrl(avatorUrl);
            responseBody.setCoverPictureUrl(coverUrl);

            responseBody.setLocation("");
            responseBody.setType(video.getType());
            liveResponseBodies.add(responseBody);
        }
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(liveResponseBodies);

        return response;
    }

    /**
     * 获取历史直播详情
     * @param videoId
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/0.1/wechat/video/{videoId}",method = RequestMethod.GET)
    public Response getVideoDetail(@PathVariable String videoId,HttpServletRequest httpServletRequest) {
        Response response=new Response<WechatVideoResponseBody>();
        Video video=videoMapper.selectByPrimaryKey(videoId);

        if(video==null){
            response.setRetureValue(LIST_IS_EMPTY);
            response.setRetureMsg(MSG_LIST_IS_EMPTY);
            return response;
        }

        WechatVideoResponseBody responseBody=new WechatVideoResponseBody();
        responseBody.setVideoId(video.getId());
        responseBody.setUserId(video.getUserId());
        responseBody.setChannelId(video.getChannelId());
        responseBody.setGroupId(video.getGroupid());

        User user=userMapper.selectByPrimaryKey(video.getUserId());
        String avatorUrl= PictureUtil.getPictureUrlFromId(user.getHeadPicId(),pictureMapper);
        String coverUrl=PictureUtil.getPictureUrlFromId(video.getCoverPictureId(),pictureMapper);

        responseBody.setAvatorUrl(avatorUrl);
        responseBody.setCoverPictureUrl(coverUrl);
        responseBody.setPlayUrl(video.getPlayUrl());
        responseBody.setVideoName(video.getTitle());
        responseBody.setUserName(user.getNickName());

        responseBody.setStartTime(video.gettStartTime()+"");
        responseBody.setEndTime(video.gettEndTime()+"");

        responseBody.setViewCount(video.getViewCount());
        responseBody.setLikeCount(video.getLikeCount());

        responseBody.setLocation("");
        responseBody.setType(video.getType());
        responseBody.setIntroduction(video.getIntroduction());

        //设置议程
        responseBody.setAgendaList(null);

        //设置嘉宾列表
        responseBody.setGuestList(null);

        responseBody.setTag(null);

        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(responseBody);
        return response;
    }


}
